#include "verify.h"

CVImage verify(const CVImage *I1, const CVImage *I2, const CVImage *Disparity)
{
    CVImage disparity_new(cvCreateImage(cvSize(I1->width(),I1->height()),IPL_DEPTH_32F,1));
    CVImage I11(cvCreateImage(cvSize(I1->width(),I1->height()),IPL_DEPTH_32F,1));
    CVImage I21(cvCreateImage(cvSize(I1->width(),I1->height()),IPL_DEPTH_32F,1));

    cvScale(*Disparity,disparity_new,-1);
    //cvConvert(*Disparity,disparity_new);
    cvConvert(I1->ToIplImage(),I11.ToIplImage());
    cvConvert(I2->ToIplImage(),I21.ToIplImage());

    CVImage mapx(cvCreateImage(cvSize(I1->width(),I1->height()),IPL_DEPTH_32F,1));
    CVImage mapy(cvCreateImage(cvSize(I1->width(),I1->height()),IPL_DEPTH_32F,1));

    for(int y=0; y < I1->height(); ++y)
        for(int x=0; x < I1->width(); ++x)
        {
            float* p = reinterpret_cast<float*>(mapx.Pix(x,y));
            *p = x + *(float*)disparity_new.Pix(x,y);

            p = reinterpret_cast<float*>(mapy.Pix(x,y));
            *p = y;
        }


    CVImage destination(cvCreateImage(cvSize(I1->width(),I1->height()),IPL_DEPTH_32F,1));
    CVImage diff (cvCreateImage(cvSize(I1->width(),I1->height()),IPL_DEPTH_32F,1));

    int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS;
    cvRemap(I11,destination,mapx,mapy,flags);
    cvAbsDiff(destination.ToIplImage(),I21.ToIplImage(),diff.ToIplImage());

    double min,max;
    cvMinMaxLoc(destination,&min,&max);
    cout << min << "  " << max << endl;

    cvMinMaxLoc(diff,&min,&max);
    cout << min << "  " << max << endl;


    destination.ShowImage("remmaped");
    diff.ShowImage("difference");
    return (diff);

}


